package com.example.skillcinema.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.skillcinema.databinding.GalleryItemBinding
import com.example.skillcinema.db.responses.ImageItemResponse
import com.example.skillcinema.viewholders.GalleryItemViewHolder

class GalleryForPreviewRecyclerViewAdapter : RecyclerView.Adapter<GalleryItemViewHolder>() {
    private var values: List<ImageItemResponse> = listOf()
    fun setData(list: List<ImageItemResponse>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): GalleryItemViewHolder = GalleryItemViewHolder(
        GalleryItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
    )

    override fun onBindViewHolder(
        holder: GalleryItemViewHolder,
        position: Int
    ) {
        val item: ImageItemResponse = values[position]
        with(holder.binding) {
            Glide.with(galleryImageView)
                .load(item.imageUrl)
                .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                .error(android.R.drawable.stat_notify_error)
                .into(galleryImageView)
        }
    }

    override fun getItemCount(): Int = values.count()
}